<?php
class city extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->tit = '城市';

		$this->modelObj = D('sys_city');

	}
    
	//列表
	public function index(){
		$this->tabtit = array('全部','禁用');

		$parent_id = arg('parent_id')?arg('parent_id'):0;

		$cond = "parent_id={$parent_id}";

		//加入查询条件
		switch($this->tab){
			case '1':
				$cond .=" and status=0";
				break;
			default:
				$cond .=" and status=1";
		}
		//搜索
		if($this->keyword){
			$cond .=" and (name like '%".$this->keyword."%') ";
		}else{
			$cond .= " and parent_id={$parent_id}";
		}

		$this->list = $this->modelObj->getList($cond);
     
	}

	//添加
	public function add(){
		if($this->isAjax()){
			$data = $this->postData('data');
			// $data['company_id'] = $this->company['id'];
			$this->modelObj->add($data);
			$this->renderSuccess('添加成功', "/{$this->routeUrl}/index");
		}
		$this->info = array('status'=>1,'sort'=>100);
		$this->display($this->routeUrl."/set.php");
	}

	//编辑
	public function edit(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');
			$this->modelObj->edit($id,$data);
			$this->renderSuccess('编辑成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);
		$this->display($this->routeUrl."/set.php");
	}

	//审核
	public function act()
	{
		$id = arg('id');
		$status = arg('status');
		$msg = ($status==1)?'显示':'隐藏';
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->act($id,$status);
				$this->renderSuccess($msg.'成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->act($val,1);
					}
					$this->renderSuccess('显示成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
		}
	}

    //删除
	public function del()
	{
		$id = arg('id');
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->del($id);
				$this->renderSuccess('删除成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->del($val);
					}
					$this->renderSuccess('删除成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
			
		}
	}

	//隐藏
	public function ban()
	{
		$opid = arg('opid');
		if($this->isAjax()){
			if($opid){
				foreach($opid as $val){
					$res = $this->modelObj->act($val,0);
				}
				$this->renderSuccess('隐藏成功');
			}
			$this->renderError('请选择数据');
		}
	}
	
	//排序
	public function sort()
	{
        $sort = arg('sort');
		$result = $this->modelObj->sort($sort);
		$this->renderSuccess('排序成功');
	}
	
	//分站
	public function site_act(){
		$id = arg('id');
		$status = arg('status');
		if($status==1){
			$msg = '开启';
		}else{
			$msg = '关闭';
		}
		if($this->isAjax()){
			$res = $this->modelObj->site_act($id,$status);
			$this->renderSuccess($msg.'成功');
		}
	}

}